﻿init();

function init() {
    var Data;
    var old_selectd_id = 0;

    if (window.localStorage.menu_index == undefined || window.localStorage.menu_index == "") {
        window.localStorage.menu_index = 0;
    }
    else {
        old_selectd_id = Number(window.localStorage.menu_index);
    }

    chrome.bookmarks.getTree(function (data, bookmarkArray, path) {
        console.log(data);
        Data = data[0].children[0].children;
        SetMenu();
        SetMain(old_selectd_id);
        $("#menu_" + old_selectd_id).addClass("selectd");
    });

    function SetMenu() {
        for (var index in Data) {
            var id = "menu_" + index;
            var url = Data[index].url;

            if (url == undefined) {
                if (old_selectd_id <= 0) {
                    old_selectd_id = index;
                }
                $("#left_ul").append("<li id=\"" + id + "\" title=\"" + index + "\">" + Data[index].title + "</li>");
                document.getElementById(id).addEventListener("click", function (e) {
                    onClickMenu(e);
                });
            }
        }
    }

    function onClickMenu(e) {
        var id = e.currentTarget.title;

        $("#menu_" + old_selectd_id).removeClass("selectd");
        $("#menu_" + id).addClass("selectd");
        old_selectd_id = id;
        window.localStorage.menu_index = id;

        SetMain(id);
    }

    var html_temp = "";
    function SetMain(index) {
        var html = "";
        var _arr = Data[index].children;

        //目录下导航
        for (var _index in _arr) {
            html_temp = "";
            var title = _arr[_index].title;
            var url = _arr[_index].url;
            var show_title = cutStr(title, 20);

            if (url == undefined) {
                html += "<div class=\"jj-list\"><div class=\"jj-list-tit\" title='" + title + "'>" + show_title + "</div><ul class='jj-list-con _b'>";
                html += GetAllUrl(_arr[_index]);
                html += "</ul></div>";
            }
        }

        //根目录导航
        html += "<div class=\"jj-list\"><div class=\"jj-list-tit\" title='默认'>默认</div><ul class='jj-list-con _b'>";
        for (var _index in _arr) {
            var title = _arr[_index].title;
            var url = _arr[_index].url;
            var show_title = cutStr(title, 20);

            if (url != undefined) {
                var temp = "<li><a href=\"" + url + "\" title=\"" + title + "\" class=\"link-3\" target=\"_self\" rel=\"nofollow\">" + show_title + "</a></li>";
                html += temp;
            }
        }
        html += "</ul></div>";

        $("#right_main").html(html);
    }

    function GetAllUrl(data) {
        for (var children in data) {
            if (data[children].length > 0 && typeof (data[children]) == "object") {
                GetAllUrl(data[children]);
            } else {
                if (typeof (data[children]) == "object") {
                    for (var childKey in data[children]) {
                        if (typeof (data[children][childKey]) == "object") {
                            GetAllUrl(data[children][childKey]);
                        } else {
                            if (childKey === "url") {
                                var url = data[children][childKey];
                                var title = data[children].title;
                                var show_title = cutStr(title, 25);
                                html_temp += "<li><a href=\"" + url + "\" title=\"" + title + "\" class=\"link-3\" target=\"_self\" rel=\"nofollow\">" + show_title + "</a></li>";
                            }
                        }
                    }
                }
                else {
                    if (children === "url") {
                        var url = data[children];
                        var title = data.title;
                        var show_title = cutStr(title, 25);
                        html_temp += "<li><a href=\"" + url + "\" title=\"" + title + "\" class=\"link-3\" target=\"_self\" rel=\"nofollow\">" + show_title + "</a></li>";
                    }
                }
            }
        }
        return html_temp;
    }

    /*
* param str 要截取的字符串
* param L 要截取的字节长度，注意是字节不是字符，一个汉字两个字节
* return 截取后的字符串
*/
    function cutStr(str, L) {
        var result = '',
            strlen = str.length, // 字符串长度
            chrlen = str.replace(/[^\x00-\xff]/g, '**').length; // 字节长度

        if (chrlen <= L) { return str; }

        for (var i = 0, j = 0; i < strlen; i++) {
            var chr = str.charAt(i);
            if (/[\x00-\xff]/.test(chr)) {
                j++; // ascii码为0-255，一个字符就是一个字节的长度
            } else {
                j += 2; // ascii码为0-255以外，一个字符就是两个字节的长度
            }
            if (j <= L) { // 当加上当前字符以后，如果总字节长度小于等于L，则将当前字符真实的+在result后
                result += chr;
            } else { // 反之则说明result已经是不拆分字符的情况下最接近L的值了，直接返回
                return result;
            }
        }
    }
}